//
//  WSBaseRefreshCustomTableViewController.m
//  WSFramework
//
//  Created by zhangcong on 2018/4/18.
//  Copyright © 2018年 zhangcong. All rights reserved.
//

#import "WSBaseRefreshCustomTableViewController.h"
#import <MJRefresh.h>
#import <WSTableViewCell.h>

@interface WSBaseRefreshCustomTableViewController ()

@end

@implementation WSBaseRefreshCustomTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    } else {
        // Fallback on earlier versions
    }
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self setupRefreshControl];
    if ([self.tableView tableFooterView] == nil) {
        UIView *view = [UIView new];
        [self.tableView setTableFooterView:view];
        view.backgroundColor = [UIColor clearColor];
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.tableView.frame = self.view.bounds;
    });
}

- (void)setupRefreshControl {
    WS(weakSelf)
    
    MJRefreshNormalHeader * refreshHead= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        SS(strongSelf)
        [strongSelf didRequest];
    }];
//    [ShareClass setMjHeaderWhiteMode:refreshHead];
    self.tableView.mj_header = refreshHead;
    
    if ([self autoRefreshLoad]) {
        [self.tableView.mj_header beginRefreshing];
    }
}

- (BOOL)autoRefreshLoad {
    return YES;
}

- (void)didRequestComplete:(id)data {
    [self.tableView reloadData];
    [self endRefreshing];
}

- (void)didRequestError:(NSError *)error {
    [super didRequestError:error];
    [self endRefreshing];
}

- (void)dealloc
{
//    [self performSelectorRemoveRefreshControl];
}

- (void)endRefreshing {
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (NSString *)tableView:(UITableView *)tableView cellIdentifierForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = [self tableView:tableView cellIdentifierForRowAtIndexPath:indexPath];
    if ( cellIdentifier ) {
        Class<WSTableViewCellProtocol> viewClass = NSClassFromString(cellIdentifier);
        if ( [viewClass respondsToSelector:@selector(calculateHeightWithData:)]) {
            return [viewClass calculateHeightWithData:[self tableView:tableView cellDataForRowAtIndexPath:indexPath]];
        }
    }
    return tableView.rowHeight;
}

- (id)tableView:(UITableView *)tableView cellDataForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = [self tableView:tableView cellIdentifierForRowAtIndexPath:indexPath];
    if (identifier != nil) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]
                     loadNibNamed:identifier owner:self options:nil]lastObject]  ;
        }
        if ([cell conformsToProtocol:@protocol(WSUpdateProtocol)]) {
            [self tableView:tableView willUpdateCell:cell forRowAtIndexPath:indexPath];
            id data = [self tableView:tableView cellDataForRowAtIndexPath:indexPath];
            [(id <WSUpdateProtocol>) cell update:data];
        }
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView willUpdateCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark -------------------- private
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [self tableViewSetting];
    }
    return _tableView;
}


- (void)tableViewSetting {
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    } else {
        // Fallback on earlier versions
    }
    
    _tableView.estimatedRowHeight = 0;
    _tableView.estimatedSectionHeaderHeight = 0;
    _tableView.estimatedSectionFooterHeight = 0;
    
    [self.view addSubview:_tableView];

}
@end
